import { reqLogin } from '@/api/login'
// import router from '@/router'
import { setToken, getToken, removeToken } from '@/utils/auth'
import router from '@/router'
// reqVerificationCode
export default {
  namespaced: true,

  state: {
    token: getToken() || '',
    uname: '' || '大力毛',
    // 用户信息
    userInfo: {}
  },
  mutations: {
    setToken (state, val) {
      state.token = val.token
      state.userInfo = val
      state.uname = val.userName
      console.log(val)
      setToken(val.token)
      // console.log(token)
    },
    // 删除接口
    delToken (state) {
      state.token = ''
      state.uname = ''
    }

  },
  actions: {
    // 登录请求
    async logInFn (context, val) {
      // console.log(val)
      const data = await reqLogin(val)
      // console.log(data.userName)
      if (data.success) {
        console.log(data)
        // 1存入本地 2 存入仓库
        context.commit('setToken', data)
        router.push('/')
      } else {
        // 提示消息
        console.log('登录失败')
      }
    },
    // 退出请求
    exit (context) {
      context.commit('delToken')
      removeToken()
      router.push('login')
    }
  }

}
